<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/3/29
 * Time: 17:16
 */

namespace app\admin\controller;
use app\admin\logic\code\ErrorCode;
use think\Controller;
use think\facade\Cache;
use think\facade\Request;

class Base extends controller{

    protected static $nowPage;
    protected static $cacheConfig;

    public function __construct(){
        parent::__construct();
        //判断用户是否已登录
        $user = session('admin_info');
        self::$cacheConfig = Cache::get('config_temp');
        $this->assign('sys_name', self::$cacheConfig['sys_name']);
        self::$nowPage = request()->param('page', 1);
        $this->_checkAuth($user);
        $this->_getMenuList($user->role_id, $user->is_admin);

        if($_SESSION['xm_travel']['admin_info']['oid']){
            $oid = db('site_team')->where('id','=',$_SESSION['xm_travel']['admin_info']['oid'])->value('oid');
            if(!$oid && Request::instance()->controller()!='SiteTeam'){
                $this->error('您还没有绑定站点',url('site_team/bing_site'));
               // header('location:/admin/site_team/bing_site.html');
            }
        }else if($_SESSION['xm_travel']['admin_info']['site_id']){
            $sid = db('site')->where('id','=',$_SESSION['xm_travel']['admin_info']['site_id'])->value('sid');
            if(!$sid && Request::instance()->controller()!='SiteTeam'){
                $this->error('您还没有绑定站点',url('site_team/bing_site'));
                // header('location:/admin/site_team/bing_site.html');
            }
        }
    }

    public function _cacheConfig(){
        $config = Cache::get('config_temp');

    }

    /**
     * 检查用户权限
     * @param $user
     */
    public function _checkAuth($user){
        if (!$user){
            $this->redirect('Login/index');
        }
        $controller = request()->controller();
        $action = request()->action();
        $url = $controller.'/'.$action;
        $roleOperation = Cache::get('role_operation');
        $allowOperation = ['Index/index'];
        if ($roleOperation[$user->role_id]){
            $allowOperation = array_merge($allowOperation, $roleOperation[$user->role_id]);
        }
        if (Request::isAjax()){
            return true;
        }
        $url = ucfirst($this->toUnderScore($url));
        if(!in_array($url, $allowOperation) && $user->is_admin != 1){
            $this->error('您没有操作权限'.$url,url('Index/index'));
        }
    }

    //驼峰命名转下划线命名
    private function toUnderScore($str)
     {
         $dstr = preg_replace_callback('/([A-Z]+)/',function($matchs)
         {
             return '_'.strtolower($matchs[0]);
         },$str);
         return trim(preg_replace('/_{2,}/','_',$dstr),'_');
     }

    /**
     * 获取后台菜单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function _getMenuList($roleId, $isAdmin){
        bind('operation_logic','app\admin\logic\OperationLogic');
        $operationLogic = app('operation_logic');
        $menuList = $operationLogic->getAllMenu(1, false);
        if ($menuList['code'] == ErrorCode::$SUCCESS){
            if ($isAdmin){
                $tempRole = 0;
            } else{
                $tempRole = $roleId;
            }
            $this->assign('menu_list', $menuList['data'][$tempRole]['menu_tree']);
        }
    }

    public function commonStatus(){
        $id = input('id');
        $field = input('field');
        $tableName = input('table_name');
        db($tableName)->where(array('id'=>$id,$field=>'1'))->save();
    }
}